package com.briup.web.listener;

import com.briup.bean.Book;
import com.briup.bean.Category;
import com.briup.service.IBookService;
import com.briup.service.ICategoryService;
import com.briup.service.impl.IBookServiceImpl;
import com.briup.service.impl.ICategoryServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @author 王若龙
 * @CreateTime 2022-09-23 10:09
 */
@WebListener("/**")
public class IndexListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application = servletContextEvent.getServletContext();

        ICategoryService iCategoryService = new ICategoryServiceImpl();
        List<Category> allCategorys = iCategoryService.findAllCategorys();
        application.setAttribute("Categorys", allCategorys);

        IBookService bookService = new IBookServiceImpl();
        List<Book> books = bookService.findAllBooks();
        application.setAttribute("books", books);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
